using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CookComputing.XmlRpc;

/// <summary>
/// Summary description for upc
/// </summary>
namespace upcNameSpace
{
    public class upc
    {
        private IUpcData UPC = (IUpcData)XmlRpcProxyGen.Create(typeof(IUpcData));
        public upc()
        {
            //
            // TODO: Add constructor logic here
            //
            UPC.KeepAlive = false;
        }

        public string Help
        {
            get
            {
                return UPC.help();
            }
        }
        
        public structLookupEAN retrieveUPCInfo(string upc)
        {
            structLookupEAN ret = new structLookupEAN();
            int Len;

            Len = upc.Length;
            switch (Len)
            {
                case 12:
                    upc = "0" + upc;
                    goto case 13;
                case 8:
                    upc = UPC.convertUPCE(upc);
                    goto case 13;
                case 13:
                    ret = UPC.lookupEAN(upc);
                    break;
                default:
                    ret.found = false;
                    ret.description = "UPC Not Found! Must be 8, 12, or 13 characters to be a valid UPC";
                    break;
            }
            return ret;
        }
        
        public string calculateCheckDigit(string chk)
        {
            chk = "X" + chk;
            string ret = UPC.calculateCheckDigit(chk);
            return ret;
        }
    }

    [XmlRpcMissingMapping(MappingAction.Ignore)]
    public struct structLookupEAN
    {
        public string description;
        public string lastModified;
        public string upc;
        public bool isCoupon;
        public int pendingUpdates;
        public bool found;
        public string message;
        public string size;
        public bool isInventory;
        public int inInventory;
    }    
/// <summary>
/// Summary description for WebService
/// </summary>
    [XmlRpcUrl("http://dev.upcdatabase.com/rpc")]
    public interface IUpcData : IXmlRpcProxy
    {
        [XmlRpcMethod("help")]
        string help();

        [XmlRpcMethod("lookupEAN")]
        structLookupEAN lookupEAN(string ean);

        [XmlRpcMethod("calculateCheckDigit")]
        string calculateCheckDigit(string UPCE);

        [XmlRpcMethod("convertUPCE")]
        string convertUPCE(string DigUPCE);
    }

}
